package com.example.myone.Util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.example.myone.Entity.Article;
import com.example.myone.Entity.User;
import com.example.myone.Entity.UserArticleLike;
import com.example.myone.Mapper.ArticleMapper;
import com.example.myone.Mapper.UserArticleLikeMapper;
import com.example.myone.R;

public class ClickUtils {

    public static void setClickListener(final Activity activity, View view, final String toastMessage, final Runnable otherMethod) {
        view.setOnClickListener(v -> {
            if (toastMessage != null) {
                Toast.makeText(activity, toastMessage, Toast.LENGTH_SHORT).show();
            }
            if (otherMethod != null) {
                otherMethod.run();
            }
        });
    }

    public static void setClickToOtherActivity(final Context context, final Class<?> activityClass, View... views) {
        View.OnClickListener onClickListener = v -> {
            Intent intent = new Intent(context, activityClass);
            context.startActivity(intent);
        };
        for (View view : views) {
            view.setOnClickListener(onClickListener);
        }
    }

    public static void setClickToOtherActivityWithStrExtra(final Context context, final Class<?> activityClass, String field, String value, View... views) {
        View.OnClickListener onClickListener = v -> {
            Intent intent = new Intent(context, activityClass);
            intent.putExtra(field, value);
            context.startActivity(intent);
        };
        for (View view : views) {
            view.setOnClickListener(onClickListener);
        }
    }
    public static void setClickToOtherActivityWithIntExtra(final Context context, final Class<?> activityClass, String field, Integer value, View... views) {
        View.OnClickListener onClickListener = v -> {
            Intent intent = new Intent(context, activityClass);
            intent.putExtra(field, value);
            context.startActivity(intent);
        };
        for (View view : views) {
            view.setOnClickListener(onClickListener);
        }
    }


    public static void setupLikeButton(Context context, Article article, ImageView likeButton, TextView likeCount) {
        User outUser = LoginInfoUtil.getUser(context);
        boolean liked = false;
        UserArticleLike outUserArticleLike = new UserArticleLike();
        if (outUser != null) {
            outUserArticleLike.articleId = article.id;
            outUserArticleLike.userId = outUser.id;
            liked = UserArticleLikeMapper.checkLikedByUserIdAndArticleId(context, outUserArticleLike);
        }
        // 设置图片源和文本颜色
        int iconResId = liked ? R.drawable.ic_red_like : R.drawable.ic_like;
        int textColorId = liked ? R.color.red : R.color.black;
        likeButton.setImageResource(iconResId);
        likeCount.setText(String.valueOf(article.likes));
        likeCount.setTextColor(ContextCompat.getColor(context, textColorId));
        // 添加点击事件
        likeButton.setOnClickListener(v -> {
            User user = LoginInfoUtil.getUser(context);
            if (user != null) {
                UserArticleLike userArticleLike = new UserArticleLike();
                userArticleLike.userId = user.id;
                userArticleLike.articleId = article.id;
                boolean beforeLiked = UserArticleLikeMapper.checkLikedByUserIdAndArticleId(context, userArticleLike);
                boolean newLiked = !beforeLiked;
                // 更新样式
                int newIconResId = newLiked ? R.drawable.ic_red_like : R.drawable.ic_like;
                int newTextColorId = newLiked ? R.color.red : R.color.black;
                likeButton.setImageResource(newIconResId);
                likeCount.setTextColor(ContextCompat.getColor(context, newTextColorId));
                // 更新赞数
                article.likes = newLiked ? article.likes + 1 : article.likes - 1;
                ArticleMapper.updateArticle(context, article);
                likeCount.setText(String.valueOf(article.likes));
                // 更新用户赞
                if (newLiked) {
                    // 点赞
                    UserArticleLikeMapper.insertUserArticleLike(context, outUserArticleLike);
                } else {
                    // 取消点赞
                    UserArticleLike delete = new UserArticleLike();
                    delete.articleId = article.id;
                    delete.userId = user.id;
                    UserArticleLikeMapper.deleteByUserIdAndArticleId(context, delete);
                }
            } else {
                Toast.makeText(context, "请先登录", Toast.LENGTH_SHORT).show();
            }
        });
    }


    public static void setupBookButton(Context context, ImageView bookButton) {
        bookButton.setOnClickListener(v -> {
            if (!LoginInfoUtil.isLogin(context)) {
                Toast.makeText(context, "请先登录", Toast.LENGTH_SHORT).show();
                return;
            }
            Drawable drawable = bookButton.getDrawable(); // 当前的背景
            Drawable redBookDrawable = ContextCompat.getDrawable(context, R.drawable.ic_red_book); // 红色的背景
            Drawable bookDrawable = ContextCompat.getDrawable(context, R.drawable.ic_book); // 灰色的背景
            if (drawable != null && drawable.getConstantState() != null && redBookDrawable != null &&
                    drawable.getConstantState().equals(redBookDrawable.getConstantState())) {
                bookButton.setImageResource(R.drawable.ic_book); // 取消点赞
            } else if (drawable != null && drawable.getConstantState() != null && bookDrawable != null &&
                    drawable.getConstantState().equals(bookDrawable.getConstantState())) {
                bookButton.setImageResource(R.drawable.ic_red_book); // 点赞
            }
        });
    }
}

